package com.by.model;

import com.by.constant.BusinessEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author: Fsw
 * 系统统一响应结果对象
 * @date 2025-08-18 21:30
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("项目统一响应结果对象")
public class Result<T> implements Serializable {
    @ApiModelProperty("状态码")
    private Integer Code = 200;
    @ApiModelProperty("消息")
    private String msg = "ok";
    @ApiModelProperty("数据")
    private T data;

    /**
     * 操作成功
     */

    public static <T> Result<T> success(T data) {
        Result result = new Result<>();
        result.setData(data);
        return result;
    }

    /**
     * 操作失败
     */
    public static <T> Result<T> fail(Integer code, String msg) {
        Result result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(null);
        return result;
    }
    /**
     * 操作失败(枚举)
     */
    public static <T> Result<T>fail(BusinessEnum businessEnum){
        Result result = new Result();
        result.setCode(businessEnum.getCode());
        result.setMsg(businessEnum.getDesc());
        result.setData(null);
        return result;
    }
}
